package czk.concrent.enjoyModelWithNotLock.atomicRefenceFieldUpdater;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/6/14 22:10
 */
public class Test {
    public static void main(String[] args) {
        // 原子引用更新字段
        AtomicReferenceFieldUpdater updater=AtomicReferenceFieldUpdater.newUpdater(Person.class,String.class,"name");
        // 穿件对象
        Person person=new Person("陈阿坤");
        // expect和原始的必须相近，不然会导致cas失败
        System.out.println(updater.compareAndSet(person, "陈坤", "陈彰坤"));
        System.out.println(person);
    }
}
class Person{
    // volatile保证原子性
    volatile String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
